package com.open.party.cloud.view

import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.sinothk.hussars.views.scrollViewGridView.IconTextBean
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.view.comm.view.banner.CustomViewHolder
import com.open.party.cloud.view.comm.view.scrollViewGridView.FunItemAdapter
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.banner.nice.BannerConfig
import com.sinothk.banner.nice.Transformer
import com.sinothk.view.xrefresh.XRefreshLayout
import com.sinothk.view.xrefresh.listener.RefreshHandler
import kotlinx.android.synthetic.main.home_fragment_main.*
import kotlinx.android.synthetic.main.home_fragment_main_1011.*
import kotlinx.android.synthetic.main.home_fragment_main_1011.bannerView
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView2
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView3
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView4
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView5
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView7
import kotlinx.android.synthetic.main.home_fragment_main_1011.gridView9
import kotlinx.android.synthetic.main.home_fragment_main_1011.xRefreshView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Deprecated("已替换")
class HomeMainFragment1011 : HomeMainBaseFragment1011() {

    private var viewModel: ArticleViewModel? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.home_fragment_main_1011, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ArticleViewModel()

        initView()
        initItemView()
    }

    private fun initView() {
        xRefreshView.setEnableRefresh(true)
        xRefreshView.setEnableLoadMore(false)
        xRefreshView.setOnRefreshListener {
            Handler().postDelayed({ initBannerData() }, 500)
        }
//        refreshLayout.setRefreshEnable(true)
//        refreshLayout.setLoadMoreEnable(false)
//        refreshLayout.setRefreshHandler(object : RefreshHandler {
//            override fun onLoadMore(refresh: XRefreshLayout?) {
//            }
//
//            override fun onRefresh(refresh: XRefreshLayout?) {
//                Handler().postDelayed({ initBannerData() }, 500)
//            }
//        })

        initBannerData()
    }

    private fun initItemView() {
        val adapter = FunItemAdapter(activity, getIconTxt())
        gridView.adapter = adapter
        adapter.setOnSuperListener(this)

        val adapter2 = FunItemAdapter(activity, getIconTxt2())
        gridView2.adapter = adapter2
        adapter2.setOnSuperListener(this)

        val adapter3 = FunItemAdapter(activity, getIconTxt3())
        gridView3.adapter = adapter3
        adapter3.setOnSuperListener(this)

        val adapter4 = FunItemAdapter(activity, getIconTxt4())
        gridView4.adapter = adapter4
        adapter4.setOnSuperListener(this)

        val adapter5 = FunItemAdapter(activity, getIconTxt5())
        gridView5.adapter = adapter5
        adapter5.setOnSuperListener(this)

//        val adapter6 = FunItemAdapter(activity, getIconTxt6())
//        gridView6.adapter = adapter6
//        adapter6.setOnSuperListener(this)

        val adapter7 = FunItemAdapter(activity, getIconTxt7())
        gridView7.adapter = adapter7
        adapter7.setOnSuperListener(this)

//        val adapter8 = FunItemAdapter(activity, getIconTxt8())
//        gridView8.adapter = adapter8
//        adapter8.setOnSuperListener(this)

        val adapter9 = FunItemAdapter(activity, getIconTxt9())
        gridView9.adapter = adapter9
        adapter9.setOnSuperListener(this)
    }

    override fun onClick(position: Int, itemData: IconTextBean, flag: String) {
        super.onClick(position, itemData, flag)
    }

    // ==================================================================
    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    // 请求数据
    private fun initBannerData() {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = 1
        pageReq.pageSize = 5
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean("HOME_LBXW", "")
        pageReq.data = vo
        viewModel!!.getArticleListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(result: ResultInfo<AppPageData<ArticleEntity>>) {

        if ("getArticleListData" != result.eventType) {
            return
        }

        xRefreshView.finishRefresh()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data.records != null) {
                    initBannerView(result.data.records)
                } else {
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    /**
     * 轮播图
     */
    private fun initBannerView(records: MutableList<ArticleEntity>) {

        val bannerList: ArrayList<SlideshowListBean> = ArrayList()
        for (item in records) {
            bannerList.add(SlideshowListBean(item.id, item.imgUrl))
        }

        bannerView!!.setAutoPlay(true)
            .setPages(bannerList, CustomViewHolder())
            .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
            .setBannerAnimation(Transformer.Default)
            .setDelayTime(3000)
            .start()

        bannerView!!.setOnBannerClickListener { _, position ->
            val id = bannerList[position].id
            if (TextUtils.isEmpty(id)) {
                return@setOnBannerClickListener
            }
            XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
                .putStringExtra("id", bannerList[position].id)
                .startInFragment(this)
        }
    }
}